package org.woodwhales.concurrent.code23;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 写时复制容器 copy on write
 * 多线程环境下，写时效率低，读时效率高
 * 适合写少读多的环境
 *
 */
public class T02_CopyOnWriteList {
	public static void main(String[] args) {

		//new ArrayList<>(); //这个会出并发问题！
		//new Vector();
		List<String> lists = new CopyOnWriteArrayList<>();

		Random r = new Random();
		Thread[] ths = new Thread[100];

		for (int i = 0; i < ths.length; i++) {
			Runnable task = new Runnable() {
				@Override
				public void run() {
					for (int i = 0; i < 1000; i++) {
						lists.add("a" + r.nextInt(10000));
					}
				}
			};
			ths[i] = new Thread(task);
		}

		runAndComputeTime(ths);

		System.out.println(lists.size());
	}

	static void runAndComputeTime(Thread[] ths) {
		long start = System.currentTimeMillis();

		Arrays.asList(ths).forEach(t -> t.start());

		Arrays.asList(ths).forEach(t -> {
			try {
				t.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		});

		long end = System.currentTimeMillis();

		System.out.println(end - start);

	}
}

